package com.superman.view;

import com.alibaba.fastjson.JSON;
import com.superman.entity.*;
import com.superman.socket.TCPClient;
import com.superman.util.CurrentPayment;

import java.io.IOException;
import java.util.*;

public class Query {
    /**
     * 查询品牌列表
     * @return
     */
    public Vector<String> queryBrand(){
        Vector<String> brand = new Vector<>();
        try {
            List<Brand> brandList = JSON.parseArray(TCPClient.getResponse("queryBrand#"), Brand.class);
            for (Brand b:brandList){
                brand.add(b.getName());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            return brand;
        }
    }

    /**
     * 查询类型列表
     * @return
     */
    public Vector<String> queryCategory(){
        Vector<String> category = new Vector<>();
        try {
            List<Category> categoryList = JSON.parseArray(TCPClient.getResponse("queryCategory#"), Category.class);
            for (Category c:categoryList){
                category.add(c.getName());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            return category;
        }
    }

    /**
     * 查询所有汽车（用户 查不到未上架的）
     * @return
     */
    public List<Car_Brand_Category> userQueryCar(){
        List<Car_Brand_Category> list = new ArrayList<>();
        try {
            list = JSON.parseArray(TCPClient.getResponse("userQueryCar#"),Car_Brand_Category.class);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            return list;
        }
    }

    /**
     * 查询所有汽车 （管理员）
     * @return
     */
    public List<Car_Brand_Category> adminQueryCar(){
        List<Car_Brand_Category> list = new ArrayList<>();
        try {
             list = JSON.parseArray(TCPClient.getResponse("adminQueryCar#"),Car_Brand_Category.class);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            return list;
        }
    }

    /**
     * 按价格升序排序 （用户）
     * @return
     */
    public List<Car_Brand_Category> sortCarByRentASC(){
        List<Car_Brand_Category> list = userQueryCar();
        Collections.sort(list, new Comparator<Car_Brand_Category>() {
            @Override
            public int compare(Car_Brand_Category o1, Car_Brand_Category o2) {
                return (int) (o1.getRent() - o2.getRent());
            }
        });
        return list;
    }

   /**
     * 按价格升序排序 （管理员）
     * @return
     */
    public List<Car_Brand_Category> adminSortCarByRentASC(){
        List<Car_Brand_Category> list = adminQueryCar();
        Collections.sort(list, new Comparator<Car_Brand_Category>() {
            @Override
            public int compare(Car_Brand_Category o1, Car_Brand_Category o2) {
                return (int) (o1.getRent() - o2.getRent());
            }
        });
        return list;
    }

    /**
     * 按价格降序排序 （用户）
     * @return
     */
    public List<Car_Brand_Category> sortCarByRentDESC(){
        List<Car_Brand_Category> list = userQueryCar();
        Collections.sort(list, new Comparator<Car_Brand_Category>() {
            @Override
            public int compare(Car_Brand_Category o1, Car_Brand_Category o2) {
                return (int) (o2.getRent() - o1.getRent());
            }
        });
        return list;
    }

    /**
     * 按价格降序排序 （管理员）
     * @return
     */
    public List<Car_Brand_Category> adminSortCarByRentDESC(){
        List<Car_Brand_Category> list = adminQueryCar();
        Collections.sort(list, new Comparator<Car_Brand_Category>() {
            @Override
            public int compare(Car_Brand_Category o1, Car_Brand_Category o2) {
                return (int) (o2.getRent() - o1.getRent());
            }
        });
        return list;
    }

    /**
     * 按品牌查找 （用户）
     * @param brand
     * @return
     */
    public List<Car_Brand_Category> queryCarByBrand(String brand){
        List<Car_Brand_Category> myList = new ArrayList<>();
        List<Car_Brand_Category> list = userQueryCar();
        for (Car_Brand_Category cbc: list){
            if (cbc.getBrand().equals(brand)){
                myList.add(cbc);
            }
        }
        return myList;
    }

    /**
     * 按品牌查找（管理员）
     * @param brand
     * @return
     */
    public List<Car_Brand_Category> adminQueryCarByBrand(String brand){
        List<Car_Brand_Category> myList = new ArrayList<>();
        List<Car_Brand_Category> list = adminQueryCar();
        for (Car_Brand_Category cbc: list){
            if (cbc.getBrand().equals(brand)){
                myList.add(cbc);
            }
        }
        return myList;
    }

    /**
     * 按类型查找（用户）
     * @param category
     * @return
     */
    public List<Car_Brand_Category> queryCarByCategory(String category){
        List<Car_Brand_Category> myList = new ArrayList<>();
        List<Car_Brand_Category> list = userQueryCar();
        for (Car_Brand_Category cbc: list){
            if (cbc.getCategory().equals(category)){
                myList.add(cbc);
            }
        }
        return myList;
    }

    /**
     * 按类型查找（管理员）
     * @param category
     * @return
     */
    public List<Car_Brand_Category> adminQueryCarByCategory(String category){
        List<Car_Brand_Category> myList = new ArrayList<>();
        List<Car_Brand_Category> list = adminQueryCar();
        for (Car_Brand_Category cbc: list){
            if (cbc.getCategory().equals(category)){
                myList.add(cbc);
            }
        }
        return myList;
    }

    /**
     * 租车时汽车选项下拉列表
     * @return
     */
    public Vector<String> rentQueryCar(){
        Vector<String> myVector = new Vector<>();
        List<Car_Brand_Category> list = userQueryCar();
        for (Car_Brand_Category cbc: list){
            String s = cbc.getCar_number() + "-" + cbc.getModel() + "-" + cbc.getColor() + "-" +
                    cbc.getT_comments() + "-" + cbc.getBrand() + "-" + cbc.getCategory() + "-" +
                    ((cbc.getStatus() == 0)? "可借":"不可借");
            myVector.add(s);
        }
        return myVector;
    }

    /**
     * 还车时选择尚未归还的租车记录
     * @param user_id
     * @return
     */
    public List<Record_User_Car> returnQueryRecord(String user_id){
        List<Record_User_Car> list = null;
        try {
            list = JSON.parseArray(TCPClient.getResponse("queryRecordByUserIdAndNoReturnInfo#" + user_id), Record_User_Car.class);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            return list;
        }
    }

    /**
     * 用户查询个人租车记录
     * @param
     * @return
     */
    public List<Record_User_Car> userQueryRecord(String user_id) {
        List<Record_User_Car> list = new ArrayList<>();
        try {
            list = JSON.parseArray(TCPClient.getResponse("userQueryRecord#" + user_id), Record_User_Car.class);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }

    /**
     * 管理员查询所有租车记录
     * @return
     */
    public List<Record_User_Car> adminQueryRecord() {
        List<Record_User_Car> list = new ArrayList<>();
        try {
            list = JSON.parseArray(TCPClient.getResponse("adminQueryRecord#"), Record_User_Car.class);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }

    /**
     * 管理员查询指定用户的租车记录
     * @param user
     * @return
     */
    public List<Record_User_Car> queryRecordByUserID(User user) {
        List<Record_User_Car> myList = new ArrayList<>();
        List<Record_User_Car> list = adminQueryRecord();
        for (Record_User_Car ruc : list) {
            if (ruc.getUser_id().equals(user.getId())){
                myList.add(ruc);
            }
        }
        return myList;
    }

    /**
     * 管理员查询指定汽车的租用记录
     * @param car_id
     * @return
     */
    public List<Record_User_Car> queryRecordByCarID(String car_id) {
        List<Record_User_Car> myList = new ArrayList<>();
        List<Record_User_Car> list = adminQueryRecord();
        for (Record_User_Car ruc : list) {
            if (ruc.getCar_id().equals(car_id)){
                myList.add(ruc);
            }
        }
        return myList;
    }

    public String getBrandID(String brand){
        String brand_id = null;
        try {
            List<Brand> brandList = JSON.parseArray(TCPClient.getResponse("queryBrand#"), Brand.class);
            for (Brand b:brandList){
                if (b.getName().equals(brand)){
                    brand_id = b.getId();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            return brand_id;
        }
    }

    public String getCategoryID(String category){
        String category_id = null;
        try {
            List<Category> categoryList = JSON.parseArray(TCPClient.getResponse("queryCategory#"), Category.class);
            for (Category c:categoryList){
                if (c.getName().equals(category)){
                    category_id = c.getId();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            return category_id;
        }
    }
}
